package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.CategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Override
    public List<CategoryBrand> sellectalltwo() {
        List<CategoryBrand> list= categoryBrandMapper.sellectalltwo();
        return list;
    }

    @Override
    public void savealldate(CategoryBrand categoryBrand) {
        //查看品牌id和分类id是否重复
        LambdaQueryWrapper<CategoryBrand> categoryBrandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryBrandLambdaQueryWrapper.eq(CategoryBrand::getBrandId,categoryBrand.getBrandId());
        categoryBrandLambdaQueryWrapper.eq(CategoryBrand::getCategoryId,categoryBrand.getCategoryId());
        long count = this.count(categoryBrandLambdaQueryWrapper);
        if (count>0){
            throw  new ServiceException("品牌重复");
        }
        categoryBrandMapper.insert(categoryBrand);
    }
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public List<Brand> getbrands(Long categoryId) {
        //方法一
        /*List<CategoryBrand> categoryBrandList = this.list(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getCategoryId, categoryId)
                .select(CategoryBrand::getBrandId));
        if (!ObjectUtils.isEmpty(categoryBrandList)){
            //采用stream流获取品牌id,将  流中的对象转换为long
            List<Long> brandlist = categoryBrandList
                    .stream()
                    .map(CategoryBrand::getBrandId)
                    .collect(Collectors.toList());
            //根据品牌id查品牌列表
            List<Brand> selectBatchIds = brandMapper.selectBatchIds(brandlist);
            return selectBatchIds;
        }
        return null;*/
        //方法二
        return brandMapper.brandlist(categoryId);
    }
}
